package com.uroad.liaoningpos.util;

import android.text.TextUtils;

import com.uroad.lib.util.string.StringUtils;

/**
 * Created by admin(wlw) on 2018/8/9.
 */
public class SystemUtil {
    /**
     * 检查当前版本是否小于最新版本 if(currVerson < lastVerson) return true;
     * currVerson APP当前版本       1.6.1
     * lastVerson 接口返回的最新版本 1.7.8
     * */
    public static boolean isNeedUpdate(String currVerson, String lastVerson){
        try{
            if (TextUtils.isEmpty(currVerson) || TextUtils.isEmpty(lastVerson)) {
                return false;
            }
            String[] currVerson_array = currVerson.split("\\.");
            String[] lastVerson_array = lastVerson.split("\\.");
            if(currVerson_array.length == 2){
                if (currVerson_array[1].length() == 2) {
                    currVerson_array = new String[]{currVerson_array[0],
                            currVerson_array[1].substring(0, 1), currVerson_array[1].substring(1)};
                }else {
                    currVerson_array = new String[]{currVerson_array[0], currVerson_array[1], "0"};
                }
            }
            if(lastVerson_array.length == 2){
                if (lastVerson_array[1].length() == 2) {
                    lastVerson_array = new String[]{lastVerson_array[0],
                            lastVerson_array[1].substring(0, 1), lastVerson_array[1].substring(1)};
                }else {
                    lastVerson_array = new String[]{lastVerson_array[0], lastVerson_array[1], "0"};
                }
            }
            if(currVerson_array.length == 3 && lastVerson_array.length == 3){
                for(int i = 0;i<3;i++){
                    long curr = StringUtils.Convert2Long(currVerson_array[i]);
                    long last = StringUtils.Convert2Long(lastVerson_array[i]);
                    if(curr < last){
                        return true;
                    }else if(curr == last){
                        continue;
                    }else{
                        return false;
                    }
                }
            }else{
                //数据格式异常，则视为不需要更新
                return false;
            }
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }
}
